import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit

from connect_database.user_tools import UserTool
from ui.registered_window import Ui_MainWindow


# 注册页面
class RegisteredWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(RegisteredWindow, self).__init__()
        self.tools = None
        self.setupUi(self)
        self.label_3.clear()
        self.label_4.clear()
        self.label_6.clear()
        self.lineEdit_2.setEchoMode(QLineEdit.Password)
        self.lineEdit_2.setClearButtonEnabled(True)
        self.lineEdit_3.setEchoMode(QLineEdit.Password)
        self.lineEdit_3.setClearButtonEnabled(True)
        # 绑定注册按钮
        self.pushButton_2.clicked.connect(self.verify_info)
        self.lineEdit_3.returnPressed.connect(self.verify_info)

    def verify_info(self):
        # 初始化用户工具类
        self.tools = UserTool()
        self.label_3.clear()
        self.label_4.clear()
        self.label_6.clear()
        user_name = self.lineEdit.text()
        # 密码不为空
        if user_name.strip(" "):
            pass_word = self.lineEdit_2.text()
            # 验证密码是否合法
            if 0 <= len(pass_word.strip(" ")) <= 8:
                confirm_pass_word = self.lineEdit_3.text()
                # 验证密码正确
                if confirm_pass_word.strip(" ") == pass_word:
                    # 提交信息
                    self.tools.create_user(user_name, pass_word)
                    self.hide()
                else:
                    self.label_6.setText("密码不一致")
            else:
                self.label_4.setText("密码长度在0-8内")
        else:
            self.label_3.setText("用户名为空")
        self.lineEdit.clear()
        self.lineEdit_3.clear()
        self.lineEdit_2.clear()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    r1 = RegisteredWindow()
    r1.show()
    sys.exit(app.exec_())  # 程序关闭时退出进程
